package com.lrc.dentalbackend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lrc.dentalbackend.common.BaseContext;
import com.lrc.dentalbackend.common.ResponseResult;
import com.lrc.dentalbackend.domain.dto.*;
import com.lrc.dentalbackend.domain.entity.Article;
import com.lrc.dentalbackend.domain.entity.User;
import com.lrc.dentalbackend.service.ArticleService;
import com.lrc.dentalbackend.service.RecordService;
import com.lrc.dentalbackend.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @Resource
    private ArticleService articleService;

    @Resource
    private RecordService recordService;
    @ApiOperation("分页并且可通过文章标题或内容模糊查询")
    @PostMapping("/get/articles")
    private ResponseResult getPageListAndSearch(@RequestBody ArticleQueryDTO articleQuery){
        Page<Article> page = articleService.getPageListAndSearch(articleQuery);
        return ResponseResult.ok(page);
    }
    /**
     * 用户名或手机号登录
     * @param user
     * @return
     */
    @ApiOperation("用户名或手机号登录")
    @PostMapping("/login")
    public ResponseResult login(@RequestBody UserDTO user){
        return ResponseResult.ok(userService.login(user));
    }

    @ApiOperation("注册")
    @PostMapping("/register")
    public ResponseResult register(@RequestBody UserDTO user){
        userService.register(user);
        return ResponseResult.ok();
    }

    @ApiOperation("修改个人用户信息")
    @PostMapping("/updateUser")
    public ResponseResult updateUser(@RequestBody UserDTO user){
        userService.updateUser(user);
        return ResponseResult.ok();
    }
    @ApiOperation("获取个人信息")
    @PostMapping("/getUserInfo")
    public ResponseResult getUserInfo(){
        User user = userService.getById(BaseContext.getCurrentId());
        return ResponseResult.ok(user);
    }
    @ApiOperation("登出")
    @PostMapping("/logout")
    public ResponseResult logout(){
        BaseContext.removeCurrentId();
        return ResponseResult.ok();
    }

    @ApiOperation("添加使用记录")
    @PostMapping("/addRecord")
    public ResponseResult addRecord(@RequestBody RecordDTO record){
        recordService.add(record);
        return ResponseResult.ok();
    }

    @ApiOperation("根据id查找使用记录")
    @GetMapping("/getRecord/{id}")
    public ResponseResult getRecord(@PathVariable Long id){
        if (ObjectUtils.isEmpty(id)){
            throw new RuntimeException("id不能为空");
        }
        return ResponseResult.ok(recordService.getById(id));
    }

    @ApiOperation("删除记录")
    @PostMapping("/delRecord/{id}")
    public ResponseResult delRecord(@PathVariable Long id){
        if (ObjectUtils.isEmpty(id)){
            throw new RuntimeException("id不能为空");
        }
        recordService.removeById(id);
        return ResponseResult.ok();
    }

    @ApiOperation("更新记录名和照片")
    @PostMapping("/updateRecord")
    public ResponseResult updateRecord(@RequestBody RecordUpdateDTO recordUpdateDTO){
        recordService.updateRecord(recordUpdateDTO);
        return ResponseResult.ok();
    }

    @ApiOperation("查询该用户所有使用记录及根据名字搜索")
    @GetMapping("/getAllRecord")
    public ResponseResult getAllRecord(String name){
        return ResponseResult.ok(recordService.selectAllRecord(name));
    }

}
